<?php
namespace App\Http\Controllers\ApiXcx;

use App\Service\ApiXcx\UserService;
use App\Utilitys\Upload;
use Illuminate\Http\Request;

class UserController extends BaseController
{
    //用户

    public function __construct()
    {
        $this->db = new UserService();
    }

    //上传头像
    public function headUpload(Request $request)
    {
        $token = $request->post('token', '');
        $uid = $this->db->addApiCount($token);
        if (!$uid) {
            return $this->toJson(-2, '该用户不存在');
        }
        $resUpload = Upload::uploadImg($request, 'head');
        if ($resUpload['code'] < 0) {
            return $this->toJson(-2, $resUpload['msg']);
        }
        $imglink = $resUpload['data'];
        $imgSizes = getimagesize($imglink);
        $pas = [
            'link' => $imglink,
            'w' => $imgSizes[0],
            'h' => $imgSizes[1],
        ];
        $res = $this->db->setHead($uid, $pas);
        if ($res['code'] < 0) {
            return $this->toJson(-2, $res['msg']);
        }
        return $this->toJson();
    }

    //昵称修改
    public function setNick(Request $request)
    {
        $token = $request->post('token', '');
        $uid = $this->db->addApiCount($token);
        $nick = $request->post('nick', '');
        if (!$uid) {
            return $this->toJson(-2, '该用户不存在');
        }
        if (!$nick) {
            return $this->toJson(-2, '昵称必须的');
        }
        $res = $this->db->setNick($uid, $nick);
        if ($res['code'] < 0) {
            return $this->toJson(-2, $res['msg']);
        }
        return $this->toJson();
    }

    //登新人领取优惠劵
    public function addCouponByNew(Request $request)
    {
        $token = $request->post('token', '');
        $uid = $this->db->addApiCount($token);
        if (!$uid) {
            return $this->toJson(-2, '没有登录');
        }
        $res = $this->db->addCouponByNew($uid);
        if ($res['code'] < 0) {
            return $this->toJson(-2, $res['msg']);
        }
        return $this->toJson();
    }

    //等级列表
    public function getLevelList(Request $request)
    {
        $token = $request->post('token', '');
        $uid = $this->db->addApiCount($token);
        $res = $this->db->getLevelList();
        if ($res['code'] < 0) {
            return $this->toJson(-2, $res['msg']);
        }
        return $this->toJson(0, '', $res['data'], $res['total']);
    }

    //充值记录列表
    public function getRechargeList(Request $request)
    {
        $token = $request->post('token', '');
        $uid = $this->db->addApiCount($token);
        $pages['page'] = $request->post('page', 1);
        $pages['size'] = $request->post('size', 10);
        $res = $this->db->getRechargeList($pages, $uid);
        if ($res['code'] < 0) {
            return $this->toJson(-2, $res['msg']);
        }
        return $this->toJson(0, '', $res['data'], $res['total']);
    }

    //用户协议
    public function xieyi(Request $request)
    {
        $token = $request->post('token', '');
        $uid = $this->db->addApiCount($token);
        $fpath = 'xieyi.json';
        $fdetail = file_get_contents($fpath);
        if (!$fdetail) {
            return $this->toJson(-2, '没有协议内容');
        }
        $fdetail = json_decode($fdetail, true);
        return $this->toJson(0, '', $fdetail);
    }

    //用户权益
    public function quanyi(Request $request)
    {
        $token = $request->post('token', '');
        $uid = $this->db->addApiCount($token);
        $fpath = 'quanyi.json';
        $fdetail = file_get_contents($fpath);
        if (!$fdetail) {
            return $this->toJson(-2, '没有权益内容');
        }
        $fdetail = json_decode($fdetail, true);
        return $this->toJson(0, '', $fdetail);
    }

    //获取用户信息
    public function infoByToken(Request $request)
    {
        $token = $request->post('token', '');
        //$uid = $this->db->addApiCount($token);
        $res = $this->db->getOneByToken($token);
        if ($res['code'] < 0) {
            return $this->toJson(-2, $res['msg']);
        }
        return $this->toJson(0, '', $res['data']);
    }

    //我的团队成员
    public function getTeamList(Request $request) {
        $token = $request->post('token', '');
        $uid = $this->db->addApiCount($token);
        if (!$uid) {
            return $this->toJson(-2, '没有登录');
        }
        $pages['start'] = $request->post('page', 1);
        $pages['limit'] = $request->post('size', 4);
        $res = $this->db->getTeamList($pages, $uid);
        if ($res['code'] < 0) {
            return $this->toJson(-2, $res['msg']);
        }
        return $this->toJson(0, '', $res['data'], $res['total']);
    }

    //我的团队订单
    public function getTeamOrderList(Request $request) {
        $token = $request->post('token', '');
        $uid = $this->db->addApiCount($token);
        if (!$uid) {
            return $this->toJson(-2, '没有登录');
        }
        $pages['start'] = $request->post('page', 1);
        $pages['limit'] = $request->post('size', 4);
        $res = $this->db->getTeamOrderList($pages, $uid);
        if ($res['code'] < 0) {
            return $this->toJson(-2, $res['msg']);
        }
        return $this->toJson(0, '', $res['data'], $res['total']);
    }

    //签到
    public function sign(Request $request){
        $token = $request->post('token', '');
        $user = $this->db->getUserByToken($token);
        if (!$user) {
            return $this->toJson(-2, '没有登录');
        }
        $res = $this->db->sign($user);
        if ($res['code'] < 0) {
            return $this->toJson(-2, $res['msg']);
        }
        return $this->toJson(0, '', '签到成功');
    }

    //积分明细
    public function getPointsLog(Request $request) {
        $token = $request->post('token', '');
        $uid = $this->db->addApiCount($token);
        if (!$uid) {
            return $this->toJson(-2, '没有登录');
        }
        $pages['start'] = $request->post('page', 1);
        $pages['limit'] = $request->post('size', 10);
        $res = $this->db->getPointsLog($pages, $uid);
        if ($res['code'] < 0) {
            return $this->toJson(-2, $res['msg']);
        }
        return $this->toJson(0, '', $res['data'], $res['total']);
    }

    //上传二维码
    public function uploadQR(Request $request)
    {
        $token = $request->post('token', '');
        $uid = $this->db->addApiCount($token);
        if (!$uid) {
            return $this->toJson(-2, '该用户不存在');
        }
        $resUpload = Upload::uploadImg($request, 'qr_image');
        if ($resUpload['code'] < 0) {
            return $this->toJson(-2, $resUpload['msg']);
        }
        $imglink = $resUpload['data'];
        if ($imglink){
            $imgSizes = getimagesize($imglink);
            $pas = [
                'link' => $imglink,
                'w' => $imgSizes[0],
                'h' => $imgSizes[1],
            ];
            $this->db->setQR($uid, $pas);
            return $this->toJson(0, '', $pas);
        }
        return $this->toJson(-2, '上传失败');
    }

    //合成海报
    public function conflatePoster(Request $request)
    {
        $token = $request->post('token', '');
        $user_info = $this->db->getUserByToken($token);
        if (!$user_info) {
            return $this->toJson(-2, '该用户不存在');
        }
        $resUpload = $this->db->conflatePoster($user_info);
        if ($resUpload['code'] < 0) {
            return $this->toJson(-2, $resUpload['msg']);
        }
        $imglink = $resUpload['data'];
        $imgSizes = getimagesize($imglink);
        $pas = [
            'link' => $imglink,
            'w' => $imgSizes[0],
            'h' => $imgSizes[1],
        ];
        $this->db->setPoster($user_info['id'], $pas);
        $arys['link'] = env('DOMAIN') . '/' . $pas['link'];
        return $this->toJson(0, '', $arys);
    }

    //修改手机号
    public function setPhone(Request $request)
    {
//        file_put_contents('logs/wx'.date('Ymd',time()).'.log',
//            toDate().'：零售端：'.json_encode($request->all(), JSON_UNESCAPED_UNICODE).PHP_EOL,
//            FILE_APPEND);
        $token = $request->post('token', '');
        $phone = $request->post('phone', '');
        $res = $this->db->setPhone($token, $phone);
        if ($res['code'] < 0) {
            return $this->toJson(-2, $res['msg']);
        }
        return $this->toJson(0, '', $res['data']);
    }
}